	// CREATE STRATA VARIABLE

	gen strata=.

	replace strata=1 if redlight_p1==1
	replace strata=2 if redlight_p2==1
	replace strata=3 if centralmonrovia_p2==1
	replace strata=4 if redlight_p3==1
	replace strata=5 if centralmonrovia_p3==1
	replace strata=6 if claratown_p3==1
	replace strata=7 if logantown_p3==1
	replace strata=8 if newkrutown_p3==1


   gen treatment_arm = 0 if control ==1
    replace treatment_arm = 1 if tpassonly==1
    replace treatment_arm = 2 if cashassonly==1
    replace treatment_arm = 3 if tpcashass==1

   label define tarm 1 "Therapy Only" 2 "Cash Only" 3 "Tp and Cash"
   label value treatment_arm tarm

    gen stel = (round == 1) | (round == 2)
    gen ltel = (round == 5) | (round == 6)
    gen nyel = (round == 7) | (round == 8)

    /*Making sure phase identifiers are ok*/

	replace p1_e3wks=1 if phase==1 & round==1
	replace p1_e5m=1 if phase==1 & round==3
	replace p1_e7m=1 if phase==1 & round==4
	replace p1_e12m=1 if phase==1 & round==5
	replace p1_e13m=1 if phase==1 & round==6
	replace p1_e9y=1 if phase==1 & round==7
	replace p1_e10y=1 if phase==1 & round==8

	replace p2_e2wks=1 if phase==2 & round==1
	replace p2_e2w2w=1 if phase==2 & round==2
	replace p2_e12m=1 if phase==2 & round==5
	replace p2_e13m=1 if phase==2 & round==6
    replace p2_e9y=1 if phase==2 & round==7
    replace p1_e10y=1 if phase==2 & round==8

	replace p3_e2w=1 if phase==0 & round==1
	replace p3_e5w=1 if phase==0 & round==2
	replace p3_e12m=1 if phase==0 & round==5
	replace p3_e13m=1 if phase==0 & round==6
    replace p3_e9y=1 if phase==0 & round==7
    replace p1_e10y=1 if phase==2 & round==8

	gen MTEL = 0
	replace MTEL=1 if p1==1 & round==3
	replace MTEL=1 if p1==1 & round==4
		la var MTEL "Middle Endline (exclude)"

	gen STEL=(p1_e3wks==1|p2_e2wks==1|p2_e2w2w==1|p3_e2w==1|p3_e5w==1)
		la var STEL "Short-term Endline"

	gen LTEL=(p1_e12m==1|p1_e13m==1|p2_e12m==1 | p2_e13m==1 | p3_e13m==1 | p3_e12m==1)
		la var LTEL "Long-term Endline"

	gen NYEL=(p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
		la var NYEL "9 year Endline"

	gen Unfound_9YEL=0
	replace Unfound_9YEL=1 if surveyed_p1_r7_e==0 &  surveyed_p2_r7_e==0 & surveyed_p3_r7_e==0 & surveyed_p1_r8_e==0 &  surveyed_p2_r8_e==0 & surveyed_p3_r8_e==0 & r1!=1 & r2!=1 & r3!=1 & r4!=1 & r5!=1 & r6!=1
		la var Unfound_9YEL "Unfound at 9 year endline"

	gen Unfound_LTEL=0
	replace Unfound_LTEL=1 if surveyed_p1_r5_e==0 & surveyed_p1_r6_e==0 & surveyed_p2_r5_e==0 & surveyed_p2_r6_e==0 & surveyed_p3_r5_e==0 & surveyed_p3_r6_e==0 & r1!=1 & r2!=1 & r3!=1 & r4!=1 & r7!=1 & r8!=1
		la var Unfound_LTEL "Unfound at long-term endline"

	gen Unfound_STEL=0
	replace Unfound_STEL=1 if surveyed_p1_r1_e==0 & surveyed_p1_r2_e==0 & surveyed_p2_r1_e==0 & surveyed_p2_r2_e==0 & surveyed_p3_r1_e==0 & surveyed_p3_r2_e==0 & r5!=1 & r6!=1 & r3!=1 & r4!=1 & r7!=1 & r8!=1
		la var Unfound_STEL "Unfound at short-term endline"


	gen unfound = 0
	foreach x in 1 2 3 {
		foreach y in 1 2 5 6 7 8 {
			replace unfound=1 if surveyed_p`x'_r`y'_e==0 & p`x'==1 & r`y'==1
			}
		}
	foreach x in 1 {
	foreach y in 3 4 {
		replace unfound=1 if surveyed_p`x'_r`y'_e==0 & p`x'==1 & r`y'==1
		}
	}
		la var unfound "Unfound at endline"

		egen unfound_any = max(unfound),by(partid)


	qui egen unfound_wave = min(unfound) if round<=2, by(partid)

	   tempvar temp1
	   qui egen `temp1' = min(unfound) if round == 5 | round == 6, by(partid)
		qui replace unfound_wave = `temp1' if round == 5 | round == 6

       tempvar temp2
	   qui egen `temp2' = min(unfound) if round == 7 | round == 8, by(partid)
		qui replace unfound_wave = `temp2' if round == 7 | round == 8

		qui la var unfound_wave "Unfound for BOTH surveys in this wave"


bys partid: egen found_round_10yr = total(Unfound_9YEL)


gen Unfound_9YEL_allnomort = Unfound_9YEL
    replace Unfound_9YEL_allnomort = 0  if attrit_decease==1 | attrit_imprisonment==1


     lee_bounds_trimming_ado fam_econ_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado fam_asb_tyav           , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(minus)
     lee_bounds_trimming_ado mech_allexceptef_tyav  , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado fam_mental_tyav        , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado fam_identity_tyav      , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado subabuse_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(minus)
     lee_bounds_trimming_ado fam_network_tyav       , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado selfcontrolnolo_tyav   , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)
     lee_bounds_trimming_ado timepref_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)

     lee_bounds_trimming_ado fam_econ_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado fam_asb_tyav           , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(minus) condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado mech_allexceptef_tyav  , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado fam_mental_tyav        , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado fam_identity_tyav      , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado subabuse_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(minus) condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado fam_network_tyav       , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado selfcontrolnolo_tyav   , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)
     lee_bounds_trimming_ado timepref_tyav          , treatments(tpcashass tpassonly cashassonly) controls(control) attritvar(unfound_wave) roundvar(round) round(7) direc(plus)  condcontrols($base) condfe(tp_strata_alt cg_strata)

// i. CREATE ATTRITION SCENARIOS


		foreach v in fam_econ cens_fam_asb fam_asb mech_allexceptef timepref fam_mental fam_identity subabuse fam_network selfcontrolnolo{
			foreach scenario in 9010 7525 5050 1000 8020 maxmin {
				foreach t in stav ltav tyav{
					qui gen `v'_`t'`scenario' = `v'_`t'
					la var `v'_`t'`scenario' "Attrition scenario `scenario', `v'_`t'"
				}
			}
		}

				* Increasing = good
		foreach v in fam_econ  mech_allexceptef  timepref fam_mental fam_identity fam_network selfcontrolnolo{
			forv p = 0/2 {

					* short term *
					qui sum `v'_stav if phase == `p' & control != 1, d
					if r(N) {
						local min_st = r(min)
					}

					qui sum `v'_stav if phase == `p' & control == 1, d
					if r(N) {
						local max_st = r(max)
					}

					qui sum `v'_stav if phase == `p', d

					if r(N) {
						local mean_st = r(mean)
						local sd_st = r(sd)

						qui replace `v'_stav9010 = `mean_st' + .1*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav9010 = `mean_st' - .1*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stav7525 = `mean_st' + .25*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav7525 = `mean_st' - .25*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

                        qui replace `v'_stav8020 = `mean_st' + .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav8020 = `mean_st' - .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1


						qui replace `v'_stav5050 = `mean_st' + .50*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav5050 = `mean_st' - .50*`sd_st' if control != 1 & phase == `p' & unfound_wave== 1

						qui replace `v'_stav1000 = `mean_st' + `sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav1000 = `mean_st' - `sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stavmaxmin = `max_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stavmaxmin = `min_st' if control != 1 & phase == `p' & unfound_wave == 1
						}


					* long term *
					qui sum `v'_ltav if phase == `p' & control != 1, d
					if r(N) {
						local min_lt = r(min)
					}

					qui sum `v'_ltav if phase == `p' & control == 1, d
					if r(N) {
						local max_lt = r(max)
					}

					qui sum `v'_ltav if phase == `p', d

					if r(N) {
						local mean_lt = r(mean)
						local sd_lt = r(sd)

						qui replace `v'_ltav9010 = `mean_lt' + .1*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav9010 = `mean_lt' - .1*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav7525 = `mean_lt' + .25*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav7525 = `mean_lt' - .25*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

                        qui replace `v'_ltav8020 = `mean_st' + .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav8020 = `mean_st' - .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav5050 = `mean_lt' + .50*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav5050 = `mean_lt' - .50*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav1000 = `mean_lt' + `sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav1000 = `mean_lt' - `sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltavmaxmin = `max_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltavmaxmin = `min_lt' if control != 1 & phase == `p' & unfound_wave == 1

					}


					* long term *
					qui sum `v'_tyav if phase == `p' & control != 1, d
					if r(N) {
						local min_9y = r(min)
					}

					qui sum `v'_tyav if phase == `p' & control == 1, d
					if r(N) {
						local max_9y = r(max)
					}

					qui sum `v'_tyav if phase == `p', d

					if r(N) {
						local mean_9y = r(mean)
						local sd_9y = r(sd)

						qui replace `v'_tyav9010 = `mean_9y' + .1*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav9010 = `mean_9y' - .1*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav7525 = `mean_9y' + .25*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav7525 = `mean_9y' - .25*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

                        qui replace `v'_tyav8020 = `mean_st' + .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav8020 = `mean_st' - .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav5050 = `mean_9y' + .50*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav5050 = `mean_9y' - .50*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav1000 = `mean_9y' + `sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav1000 = `mean_9y' - `sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyavmaxmin = `max_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyavmaxmin = `min_9y' if control != 1 & phase == `p' & unfound_wave == 1

					}



			}
		}


					* decreasing = good
		foreach v in  cens_fam_asb fam_asb  subabuse{
			forv p = 0/2 {

					* short term *
					qui sum `v'_stav if phase == `p' & control != 1, d
					if r(N) {
						local min_st = r(max)
					}

					qui sum `v'_stav if phase == `p' & control == 1, d
					if r(N) {
						local max_st = r(min)
					}

					qui sum `v'_stav if phase == `p', d

					if r(N) {
						local mean_st = r(mean)
						local sd_st = r(sd)

						qui replace `v'_stav9010 = `mean_st' - .1*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav9010 = `mean_st' + .1*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stav7525 = `mean_st' - .25*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav7525 = `mean_st' + .25*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stav5050 = `mean_st' - .50*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav5050 = `mean_st' + .50*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

                        qui replace `v'_stav8020 = `mean_st' - .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav8020 = `mean_st' + .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stav1000 = `mean_st' - `sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav1000 = `mean_st' + `sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_stavmaxmin = `max_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stavmaxmin = `min_st'  if control != 1 & phase == `p' & unfound_wave == 1
						}


					* long term *
					qui sum `v'_ltav if phase == `p' & control != 1, d
					if r(N) {
						local min_lt = r(max)
					}

					qui sum `v'_ltav if phase == `p' & control == 1, d
					if r(N) {
						local max_lt = r(min)
					}

					qui sum `v'_ltav if phase == `p', d

					if r(N) {
						local mean_lt = r(mean)
						local sd_lt = r(sd)

						qui replace `v'_ltav9010 = `mean_lt' - .1*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav9010 = `mean_lt' + .1*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav7525 = `mean_lt' - .25*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav7525 = `mean_lt' + .25*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

                        qui replace `v'_stav8020 = `mean_st' - .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_stav8020 = `mean_st' + .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav5050 = `mean_lt' - .50*`sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav5050 = `mean_lt' + .50*`sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltav1000 = `mean_lt' - `sd_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltav1000 = `mean_lt' + `sd_lt' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_ltavmaxmin = `max_lt' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_ltavmaxmin = `min_lt'  if control != 1 & phase == `p' & unfound_wave == 1

					}


	* 10 years *
					qui sum `v'_tyav if phase == `p' & control != 1, d
					if r(N) {
						local min_9y = r(max)
					}

					qui sum `v'_tyav if phase == `p' & control == 1, d
					if r(N) {
						local max_9y = r(min)
					}

					qui sum `v'_tyav if phase == `p', d

					if r(N) {
						local mean_9y = r(mean)
						local sd_9y = r(sd)

						qui replace `v'_tyav9010 = `mean_9y' - .1*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav9010 = `mean_9y' + .1*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav7525 = `mean_9y' - .25*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav7525 = `mean_9y' + .25*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

     					qui replace `v'_tyav8020 = `mean_st' - .2*`sd_st' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav8020 = `mean_st' + .2*`sd_st' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav5050 = `mean_9y' - .50*`sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav5050 = `mean_9y' + .50*`sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyav1000 = `mean_9y' - `sd_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyav1000 = `mean_9y' + `sd_9y' if control != 1 & phase == `p' & unfound_wave == 1

						qui replace `v'_tyavmaxmin = `max_9y' if control == 1 & phase == `p' & unfound_wave == 1
						qui replace `v'_tyavmaxmin = `min_9y'  if control != 1 & phase == `p' & unfound_wave == 1

					}

			}
		}




		/*Compliance*/

			// Merge in Class Variables
	drop _merge

	// Create cluster units //

	tostring phase, replace
	gen neighborhoodphase = regneighborhood + phase
	destring phase, replace

	// Cap occupational choice hours at 99% percentile
	foreach x in illicithouravg7d_e agriculhouravg7d_e nonagwagehouravg7d_e nonagbizhouravg7d_e nonaghighhouravg7d_e			{
		qui sum `x', d
		qui replace `x' = r(p99) if `x'>r(p99) & `x'!=.
	}


	** Alternate form of compliance: attend majority of all sessions
	egen attend_n = rowtotal(attendd*) if tpassigned==1
	*Missing first week attendance data so assume everyone who attended at least one also attended first 3 sessions
	replace attend_n = attend_n + 3 if tpassigned==1 & attend_n != 0

	gen attend_80 = 1 if attend_n > 19 & attend_n < .
	replace attend_80 = 0 if attend_n <= 19
